home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-11 | 5.7 KB | 254 lines | [TEXT/ttxt] |
- OpenDoc™ Recipes
-
- Parameter passing in OpenDoc
- By The OpenDoc Design Team
- July 11, 1995.
-
- © 1993-1995 Apple Computer, Inc. All Rights Reserved.
- Apple, the Apple logo, and Macintosh are registered trademarks of Apple Computer, Inc.
- Mac and OpenDoc are trademarks of Apple Computer, Inc.
-
- Changes since DR2
-
- 1) Removed sequence/inout.
-
- Overview
-
- This document outlines the cases where parameter passing can be problematic:
- - Object Reference
- - string/in, string/out, string/return
- - sequence/in, sequence/out, sequence/return
-
- string/inout is not discussed here because there does not seem to be any need for it.
-
- Object Reference
-
- - Do not use ODObject/inout.
-
-
- string or ODISOString
-
- 1) in parameter:
-
- Example: void StorageUnit::SetType(in ODValueType valueType);
-
- Client: Provides and releases the storage for the in parameter.
-
- // First case using constant
- const ODValueType kMyType = “MyValueType”;
- su->SetType(ev, kMyType);
- // In this case, we don’t need to explicitly dispose of the string.
- .....
-
- // Second case using heap memory
- ODULong strLength = ODISOStrLength(kMyType) + 1;
- ODPtr buffer = ODNewPtr(strLength);
- memcpy(kMyType, buffer, strLength);
- su->SetType(ev, buffer);
- // In this case, we have to dispose of the string
- ODDisposePtr(buffer);
-
- OpenDoc: Cannot store the incoming pointer. If it needs to store the string, it has to make a copy.
-
- SOM_Scope void SOMLINK ODStorageUnitSetType(ODStorageUnit *somSelf, Environment *ev,
- ODType type)
- {
- ......
-
- ODISOStrCopy(type, _fMyType);
-
- // Do NOT dispose of memory.
- }
-
-
- 2) Out parameter
-
- Example: ODBoolean ODSession::GetType(in ODTypeToken token, out ODType type);
-
- Client: Releases the storage for the out parameter.
-
- ODType type = kODNULL;
-
- // Call OpenDoc
- ODBoolean boolean = session->GetType(ev, token, &type);
-
- // Use it.
- .......
-
- // Client is responsible for disposing the out parameter.
- ODDisposePtr(type);
-
- OpenDoc: OpenDoc provides the storage for the out parameter.
-
- SOM_Scope ODBoolean SOMLINK ODBaseSessionGetType(ODBaseSession *somSelf, Environment *ev,
- ODTypeToken token, ODType* returnType)
- {
- ....
-
- // Look up type using token
- ODType myType = ......
-
- // Allocate memory for the returned type.
- // Make sure that you make a copy of it before returning.
- *returnType = ODNewPtr(sizeOfMyType);
-
- // Copying the type
- ODISOStrCopy(myType, *returnType);
-
- return kODTrue;
- }
-
-
- 3) function return
-
- Example: ODValueType ODStorageUnit::GetType();
-
- Client: Releases the storage for the function return.
-
- // Call OpenDoc
- ODValueType valueType = su->GetType(ev);
-
- // Use it.
- .......
-
- // Client is responsible for disposing the returned result.
- ODDisposePtr(valueType);
-
- OpenDoc: OpenDoc provides the storage for the returned result.
-
- SOM_Scope ODValueType SOMLINK ODStorageUnitGetType(ODStorageUnit *somSelf, Environment *ev)
- {
- ......
-
- // Allocate memory for the returned type.
- ODValueType returnValueType = ODNewPtr(sizeOfMyValueType);
-
- // Copying the value type
- ODISOStrCopy(myValueType, returnValueType);
-
- // Return the value type
- return returnValueType;
- }
-
-
- sequence
-
- 1) in parameter:
-
- Example: void ODStorageUnit::SetValue(in ODByteArray value);
-
- Client: Provides and releases the storage for the in parameter.
-
- ODByteArray ba;
- ba._length = length;
- ba._maximum = maximum;
- ba._buffer = buffer;
- su->SetValue(ev, &ba);
-
- // Client is also responsible for deallocating the memory for the buffer.
- // That means the buffer is not consumed by the call.
-
- OpenDoc: Cannot store the incoming pointer. If it needs to store the string, it has to make a copy.
-
- SOM_Scope void SOMLINK ODStorageUnitSetValue(ODStorageUnit *somSelf, Environment *ev,
- ODByteArray* value)
- {
- ......
-
- memcpy(value->_buffer, _fMyBuffer, value->_length);
-
- // Do NOT dispose of memory.
- }
-
- 2) Out parameter
-
- Example: ODBoolean ODValueNameSpace::GetEntry(in ODISOStr key, out ODByteArray value);
-
-
- Client:
- -provides the storage for the structure which contains the description of the sequence.
- - manages release of the storage and the descriptor.
-
- // Create byte array
- ODByteArray ba;
-
- // Call OpenDoc
- valueNameSpace->GetValue(ev, key, &ba);
-
- // Use data returned by OpenDoc
- ......
-
- // Dispose the buffer returned by OpenDoc.
- ODDisposePtr(ba._buffer);
-
- // ODByteArray struct gets disposed when the function exits.
-
- OpenDoc:
- - prvoides storage for the values returned
- - puts the pointers to this storage in the descriptor structures.
-
- SOM_Scope ODBoolean SOMLINK ODValueNameSpaceGetEntry(ODValueNameSpace *somSelf,
- Environment *ev, ODISOStr key, ODByteArray* value)
- {
- // Look up the entry using key
- ......
-
- // determine actual length to be returned
- actualLength = ......
-
- // Allocate buffer for data to be returned.
- buffer = ODNewPtr(actualLength);
-
- // Set the return byte array correctly.
- value->_length = actualLength;
- value->_maximum = actualLength;
- value->_buffer = buffer;
-
- .....
-
- return kODTrue;
- }
-
- 3) Return result
-
- Example: ODByteArray ODLinkSpec::GetPartData();
-
- Client:
- -provides the storage for the structure which contains the description of the sequence.
- - manages release of the storage and the descriptor.
-
- // Call OpenDoc
- ODByteArray ba = linkSpec->GetPartData(ev);
-
- // Use data returned by OpenDoc
- ......
-
- // Dispose the buffer returned by OpenDoc when done.
- ODDisposePtr(ba._buffer);
-
- OpenDoc:
- - prvoides storage for the values returned
- - puts the pointers to this storage in the descriptor structures.
-
- SOM_Scope ODByteArray SOMLINK ODLinkSpecGetPartData(ODStorageUnit *somSelf, Environment *ev)
- {
- ......
- // set up the local byte array
- ODByteArray ba;
-
- // determine actual length to be returned
- actualLength = ......
-
- // Allocate buffer for data to be returned.
- buffer = ODNewPtr(actualLength);
-
- // Set the return byte array correctly.
- ba._length = actualLength;
- ba._maximum = actualLength;
- ba._buffer = buffer;
-
- .....
-
- return ba;
- }
-